//
//  SearchViewController.m
//  FourthProject
//
//  Created by mac on 16/6/2.
//  Copyright © 2016年 cry. All rights reserved.
//

#import "SearchViewController.h"

@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
{
    NSMutableArray              *_historyArray;
    
}
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    _historyArray = [NSMutableArray arrayWithArray:self.recommendArray];
    
    
//    self.searchDisplayController.searchResultsTableView ;
    [self.myTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.recommendArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.myTable) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.textLabel.text = self.recommendArray[indexPath.row];
        return cell;
    }else {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            cell.textLabel.text = self.recommendArray[indexPath.row];
            
        }
        return cell;
    }
 
}



- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: @{@"client_id":@"qyer_android",@"client_secret":@"9fcaae8aefc4f9ac4915",@"v":@"1",@"track_deviceid":@"869070029694844",@"track_app_version":@"6.9.0",@"track_app_channel":@"xiaomi",@"track_device_info":@"lte26007",@"track_os":@"Android4.4.4",@"app_installtime":@"1463542775388",@"lat":@"24.548208",@"lon":@"118.1244"}];
    [dic setObject:searchText forKey:@"keyword"];
    [manager GET:@"http://open.qyer.com/qyer/search/autocomplate" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([searchText isEqualToString:@""]) {
            self.recommendArray = [NSMutableArray arrayWithArray:_historyArray];
            return;
        }
        
        NSLog(@"searchVar ====%@",responseObject);
        NSMutableArray *tempArr = [[NSMutableArray alloc] init];
        for (NSDictionary *ddd in responseObject[@"data"][@"entry"]) {
            NSString *str = ddd[@"cnname"];
            [tempArr addObject:str];
        }
        [self.recommendArray removeAllObjects];
        self.recommendArray = [NSMutableArray arrayWithArray:tempArr];
        [self.searchDisplayController.searchResultsTableView reloadData];
        NSLog(@"-----%@",tempArr);
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"!!! ==== %@",error);
    }];
    
}




- (IBAction)backButtonClick:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
